# 解决docker 把iptables规则drop掉的问题（Docker才需要）
#vim /usr/lib/systemd/system/docker.service
#[Service]
#ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT  #让docker允许，添加下面这句
#ExecStartPost=/sbin/iptables -P FORWARD ACCEPT

#docker system prune -a -f
#service docker restart
#docker info | grep Cgroup

# **************************************修改IPv规则**************************************  
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
#echo "net.bridge.bridge-nf-call-arptables = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv4.ip_forward = 1" >>/usr/lib/sysctl.d/50-default.conf
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
sysctl -w net.ipv4.ip_forward=1
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
sysctl --system
sysctl -p

# **************************************设置swap不开机启动**************************************
swapoff -a
sed -ie "s|/dev/mapper/centos-swap|#/dev/mapper/centos-swap|g " /etc/fstab
sed -ie "s|/dev/mapper/cl-swap|#/dev/mapper/cl-swap|g " /etc/fstab

# **************************************添加k8s源**************************************
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
#aarch64
#baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-aarch64/
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# **************************************移除podman组件，会冲突**************************************
dnf remove podman -y
yum remove buildah -y
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
iptables --flush
iptables -tnat --flush
iptables -P FORWARD ACCEPT

# **************************************安装containerd**************************************
cd /home/
wget https://download.fastgit.org/containerd/containerd/releases/download/v1.6.6/cri-containerd-cni-1.6.6-linux-amd64.tar.gz
tar -C / -xzf cri-containerd-cni-1.6.6-linux-amd64.tar.gz
systemctl enable containerd

# 配置containerd
mkdir /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sed -i 's/            SystemdCgroup = false/            SystemdCgroup = true/' /etc/containerd/config.toml
sed -i -e "s|k8s.gcr.io|k8simage|g " /etc/containerd/config.toml
systemctl daemon-reload
systemctl restart containerd

# **************************************安装三大件**************************************
dnf remove kubectl kubelet kubeadm -y
yum remove -y nfs-utils
sudo yum install -y nfs-utils wget
dnf install kubectl-1.23.8-0 kubelet-1.23.8-0 kubeadm-1.23.8-0 -y
systemctl enable kubelet.service
systemctl is-enabled kubelet.service

# **************************************配置K8S**************************************
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

sysctl --system
systemctl daemon-reload
systemctl restart containerd
systemctl restart kubelet

kubectl version
kubeadm version
kubelet --version